<?php
namespace app\index\controller;
use think\Cache;
use think\Controller;
use think\Request;
use app\index\service\LoginService;
use app\index\service\WxchatService;
use think\Db;

class Login extends Base
{
    /**
     * @author zheng
     * @date 2019/2/22
     * 登录入口
     */

    public function login(){
        $request = $this->request;
        $code = $request ->param('code');

        if(empty($code)){
            return json(['status'=>0,'msg'=>'非法code']);
        }

        $wxlogin = new LoginService($this ->appid,$this->appkey);//实例化

        $rd_seeion = $wxlogin -> get_key($code);
        if(isset($rd_seeion['status'])){
            return json($rd_seeion);
        }

        return json(['status' => 200, 'msg' => '获取成功','dat'=>$rd_seeion]);
    }

    /***
     * @return string
     * @author zheng
     * @date 2018/02/22
     * 处理小程序加密的电话
     */
    public function dephone()
    {

        $request = $this->request;
        $sing = $request->param('encryptedData');
        $iv = $request->param('iv');
        $rd_session = $request->param('rd_session');

        if (empty($sing) || empty($iv)) {
            return ['status' => 0, 'msg' => '非法参数'];
        }

        $wxphone = new WxchatService($this->appid);

        $data = $wxphone->getphone($sing, $iv,$rd_session);
        if(isset($data['status'])){
            return json($data);
        }
        $data = json_decode($data, true);

        return $this->upUser($data,$rd_session);

    }

    /**
     * 添加用户 更新用户
     */
    public function upUser($data,$rd_session){
        if (empty($data['purePhoneNumber'])) {//解密失败
            return json(['status' => 0, 'msg' => '电话解密失败']);//拉取失败
        }

        $where = array(
            'mobile' => $data['purePhoneNumber'],
            'wxapp_id' => $data['watermark']['appid']
        );

        $user = Db::name('city_user')->where($where)->find();//拉取角色信息

        if (!empty($user)) {
            if ($user['delete_flag'] == 1) {
                return json(['status' => 0, 'msg' => '无权限登录']);//拉黑用户无法操作。
            }
        }

        $add_data = array(
            'mobile' =>$data['purePhoneNumber'],
            'wxapp_id' =>$data['watermark']['appid'],
            'openid' => cache($rd_session)['openid'],
            'rd_session' => cache($rd_session)['rd_session'],
            'create_time' => date('Y-m-d H:i:s')
        );

        if($user){
            $up_user = Db::name('city_user')->where($where)->update($add_data);//拉取角色信息

        }else{
            $up_user = Db::name('city_user')->insert($add_data);//拉取角色信息
        }

        if(!$up_user){
            return json(['status' => 0, 'msg' => '登入失败']);
        }
        $data = array(
            'rd_session'=>$add_data['rd_session']
        );

        return json(['status' => 200, 'msg'=>'登录成功',$data]);
    }

    /**
     * 测登录信息
     */
    public function getMsg(){
        $dat = input();
        $rd_session = $dat['rd_session'];
        print_r(cache($rd_session));
        die;
    }
}